﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Press.MVC.Web
{
    public class GlobalApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            // Default Route
            routes.MapRoute(
                "Default",                                              // Route name
                "home/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" } // Parameter constraints
            );

            // Web Installer Route
            routes.Add(new Route("Install/{action}", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "WebInstall", action = "Index" }),
            });

            // User Routes
            routes.Add(new Route("user/{action}", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "User", action = "Index" }),
            });

            // Blog Routes

            routes.Add(new Route("{blogname}/b/{action}", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "Blog", blogname = (string)null, action = "Index" }),
            });

            routes.Add(new Route("{blogname}/b/{action}/{postid}", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "Blog", blogname = (string)null, action = "ShowPost", postid = (string)null }),
            });
            
            // Blog Admin Routes
            routes.Add(new Route("{blogname}/admin/{action}", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "BlogAdmin", blogname = (string)null, action = "Index" }),
            });

            // Mta Admin Routes
            routes.Add(new Route("mta/{action}", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "Mta", action = "Index" }),
            });  
        }

        /// <summary>
        /// Application_Start
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Application_Start(object sender, EventArgs e)
        {
            // Phill Haack: http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
            //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);

            // Register Routes in MVC
            RegisterRoutes(RouteTable.Routes);

            //StructureMap Dependency Injection
            Bootstrapper.ConfigureStructureMap();

            ControllerBuilder.Current.SetControllerFactory( new Press.MVC.Web.Controllers.StructureMapControllerFactory() );           
        }
    }
}